package cn.wxl.mymeituan.adapter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wxl.mymeituan.R;
import cn.wxl.mymeituan.activity.ShopDetailActivity;
import cn.wxl.mymeituan.bean.ShopBean;
import com.bumptech.glide.Glide;

import java.util.List;


public class ShopAdapter extends BaseAdapter {
    private Context context;
    private List<ShopBean> sbl;
    public static ShopBean specialBean1;
    public static ShopBean specialBean2;
    public static ShopBean specialBean3;

    public ShopAdapter(Context context) {
        this.context = context;
    }
    /**
     * 设置数据更新界面
     */
    public void setData(List<ShopBean> sbl) {
        this.sbl = sbl;
        notifyDataSetChanged();
    }
    /**
     * 获取Item的总数
     */
    @Override
    public int getCount() {
        return sbl == null ? 0 : sbl.size();
    }
    /**
     * 根据position得到对应Item的对象
     */
    @Override
    public ShopBean getItem(int position) {
        return sbl == null ? null : sbl.get(position);
    }
    /**
     * 根据position得到对应Item的id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 得到相应position对应的Item视图，position是当前Item的位置，
     * convertView参数是滚出屏幕的Item的View
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        //复用convertView
        if (convertView == null) {
            vh = new ViewHolder();
            convertView=LayoutInflater.from(context).inflate(R.layout.shop_item,null);
            vh.shop_name = (TextView) convertView.findViewById(R.id.shop_name);
            vh.food_monthSale = (TextView) convertView.findViewById(R.id.food_monthSale);
            vh.or_cost = (TextView) convertView.findViewById(R.id.or_cost);
            vh.shop_evaluation = (TextView) convertView.findViewById(R.id.shop_evaluation);
            vh.shop_diliver_time = (TextView) convertView.findViewById(R.id.shop_deliver_time);
            vh.shop_image = (ImageView) convertView.findViewById(R.id.shop_image);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //获取position对应的Item的数据对象
        final ShopBean shopBean = getItem(position);
        ShopBean bean1 = getItem(0);
        ShopBean bean2 = getItem(2);
        ShopBean bean3 = getItem(4);
        specialBean1 = bean1;
        specialBean2 = bean2;
        specialBean3 = bean3;
        if (shopBean != null) {
            vh.shop_name.setText(shopBean.getShopName());
            vh.food_monthSale.setText("月售" + shopBean.getMonthSale());
            vh.or_cost.setText("起送￥" + shopBean.getOfferPrice() + " | 配送￥" +
                    shopBean.getDistributionCost());
            vh.shop_diliver_time.setText(shopBean.getDeliverTime());
            vh.shop_evaluation.setText(shopBean.getEvaluation());
            Glide.with(context)
                    .load(shopBean.getShopImage())
                    .into(vh.shop_image);
        }
        //每个Item的点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到店铺详情界面
                if (shopBean == null) return;
                Intent intent = new Intent(context, ShopDetailActivity.class);
//把店铺的详细信息传递到店铺详情界面
                intent.putExtra("shop", shopBean);
                context.startActivity(intent);

            }
        });
        return convertView;
    }
    class ViewHolder {
        public TextView shop_name, food_monthSale, or_cost, shop_evaluation, shop_diliver_time;
        public ImageView shop_image;
    }
}
